# Contributor: lauren n. liberda <lauren@selfisekai.rocks>
maintainer="lauren n. liberda <lauren@selfisekai.rocks>"
pkgname=element-desktop
pkgver=1.11.84
pkgrel=0
pkgdesc="Secure and independent communication, connected via Matrix"
url="https://element.io/"
arch="aarch64 x86_64" # same as electron
license="Apache-2.0"
depends="
	electron
	font-inconsolata
	font-inter
	font-nunito
	font-opensans
	font-twemoji
	"
makedepends="
	cargo
	electron-dev
	electron-tasje
	jq
	libsecret-dev
	nodejs
	npm
	python3
	py3-setuptools
	sqlcipher-dev
	swc
	yarn
	"
source="
	https://github.com/vector-im/element-desktop/archive/refs/tags/v$pkgver/element-desktop-$pkgver.tar.gz
	https://github.com/vector-im/element-web/archive/refs/tags/v$pkgver/element-web-$pkgver.tar.gz

	add-alpine-targets.patch
	use-system-headers.patch
	tasje-one-hak.patch
	tasje-no-fuses.patch
	use-system-fonts.patch.web

	element-desktop
	"
options="net !check" # broken

# secfixes:
#   1.11.30-r0:
#     - CVE-2023-30609
#   1.11.26-r0:
#     - CVE-2023-28103
#     - CVE-2023-28427
#   1.11.7-r0:
#     - CVE-2022-39249
#     - CVE-2022-39250
#     - CVE-2022-39251
#     - CVE-2022-39236
#   1.11.4-r0:
#     - CVE-2022-36059
#     - CVE-2022-36060

# used by buildscripts (at least web's webpack)
export VERSION=$pkgver

export CARGO_PROFILE_RELEASE_OPT_LEVEL=2
export CARGO_PROFILE_RELEASE_STRIP="symbols"
export NODE_OPTIONS="--openssl-legacy-provider"

prepare() {
	default_prepare

	msg "Applying more patches"
	for x in $source; do
		case "$x" in
		*.patch.web)
			msg "$x"
			patch -p1 -i "$srcdir"/$x -d "$srcdir"/element-web-$pkgver
			;;
		esac
	done

	rm -rf res/fonts

	(
		cd "$srcdir"/element-web-$pkgver

		msg "Fetch element-web dependencies"
		yarn install --frozen-lockfile --ignore-scripts --ignore-engines
		jq '.show_labs_settings = true' < config.sample.json > config.json
	)

	ln -s "$srcdir"/element-web-$pkgver/webapp webapp

	msg "Fetch element-desktop dependencies"
	yarn install --frozen-lockfile --ignore-scripts
}

build() {
	(
		cd "$srcdir"/element-web-$pkgver

		msg "Build element-web"
		NODE_ENV=production yarn build
	)

	msg "Build element-desktop"

	yarn asar-webapp

	# add "optional" native dependencies
	# hak stands for hack
	yarn run hak --target "$(uname -m)-alpine-linux-musl"

	# stripping in build because it gets into asar
	strip node_modules/keytar/build/Release/keytar.node

	yarn build:ts

	yarn build:res

	# we need it as js to be of any use for tasje.
	# fails with `yarn tsc`. https://github.com/electron-userland/electron-builder/issues/7961
	swc compile electron-builder.ts --out-file electron-builder.mjs

	yarn install --frozen-lockfile --ignore-scripts --production

	tasje -c electron-builder.mjs pack
}

check() {
	(
		cd "$srcdir"/element-web-$pkgver

		yarn test
	)
}

package() {
	local resources="dist/resources"

	install -Dm644 $resources/app.asar "$pkgdir"/usr/lib/element-desktop/app.asar
	install -Dm644 webapp.asar "$pkgdir"/usr/lib/element-desktop/webapp.asar

	cp -r $resources/app.asar.unpacked "$pkgdir"/usr/lib/element-desktop/app.asar.unpacked

	install -Dm644 $resources/img/element.png "$pkgdir"/usr/lib/element-desktop/img/element.png

	install -Dm755 "$srcdir"/$pkgname "$pkgdir"/usr/bin/$pkgname

	install -Dm644 dist/$pkgname.desktop "$pkgdir"/usr/share/applications/$pkgname.desktop
	while read -r size; do
		install -Dm644 dist/icons/$size.png "$pkgdir"/usr/share/icons/hicolor/$size/apps/$pkgname.png
	done < dist/icons/size-list
}

sha512sums="
36e342071963e47661f69a165434457cac2cc554d581d0a60c11cedb2d8891ac06f553ba18d743f7d4652f4dcf2b9e32765b513cae4c052c5f72058e99c8cf5f  element-desktop-1.11.84.tar.gz
d49acd2a1db6960b1b3740cf87c0d5d2232428784fa2d1c2adec77694477e2cc83269a544d82f80dc0d67cc3893361d0b4a50acf584a85c431d4d148cedf0073  element-web-1.11.84.tar.gz
4747893ed3e43d3074e9afe1cdd668a6be0de073d439205fe8c38c5e0f4091cc76e3cd15d98818bea5139add29501d8d07e83c58e9da230a4ce5bb538d388f80  add-alpine-targets.patch
755b17f7b828eb6920c06a6950ad4e14c32c99d22e9c05fcef7a081b5d2034adb03db3958aa5209c99fb7201f4d888c2383fc9864c5e743dd33f8b5c4925acd7  use-system-headers.patch
92e69817fdc71f60c5c7dcbd3c7b13428cc18141cf5f27720326390f6817bec85fb1c60f8016b3a8fa275f601b16f646cda12b5e379a349368eef2f801b4de7a  tasje-one-hak.patch
876d40639305d5258089069a01e218a2f14c32efccc3130f06398e8b4cd63bc740909162954a58ee11b909dc5b3e87c3383d73727aa13aa2d7093c9c63f04057  tasje-no-fuses.patch
02953180ca58c14f4d8766bd2637d96553bbb9e96b9fcc3d2007c17a45e20e66c559d6c80ca1088c961f3a76d8ab767958fb1c37e853b811881b8e9cc546126a  use-system-fonts.patch.web
afc588311dc3b566a754e3e7fe6b37b99a06d47b8bbce0ed9acca8ef308fdab0bd1d41b406199e5cbdd86bdce695ff847cd8668857a235cbdc292ad8b899c063  element-desktop
"
